package com.googlecode.fs_thumbs;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.MessageFormat;

import com.googlecode.fs_thumbs.FsCollection.FsException;
import com.googlecode.fs_thumbs.FsCollection.Image;
import com.googlecode.fs_thumbs.FsCollection.Waypoint;

public class CurlCommands extends Main {

	public CurlCommands(final String[] args, final FsCollection fsCollection) //
			throws FileNotFoundException, IOException, FsException, FsException, FsException {
		super(args);
		printNode(fsCollection);
	}

	private void printNode(final FsCollection fsCollection) throws FsException, FsException, IOException, FsException {

		for (final Waypoint waypoint : fsCollection.getChildReferences()) {
			final FsCollection child = fsCollection.getCollection(waypoint.getId());
			if (waypoint.isLeaf()) {
				String folder = "";
				for (final Waypoint crumb : child.getCrumbs()) {
					folder += crumb.getName() + "/";
				}
				folder += child.getName();
				final String flatFolder = folder.replaceAll("[^a-zA-Z0-9-_./]+", "_").replaceAll("[_][/]", "/");
				final String shortFolder = flatFolder.replaceAll("([a-zA-Z]{1,3})[a-zA-Z0-9]*", "$1");
				final Image img = child.getImages().get(0);
				final String first = img.getImageNumber();
				final int last1 = Integer.parseInt(first) + child.getChildCount() - 1;
				final String last = "00000" + last1;
				System.out.println(MessageFormat.format(imageLink, first, last.substring(last.length() - 5),
						img.getDgsNumber(), flatFolder, shortFolder, folder));
			} else {
				printNode(child);
			}
		}
	}
}
